using System;
using System.Collections.Generic;
using System.Text;
using WCSFContrib.PageFlow;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace PageFlowQuickstart.Modules.Customers.Tests.Mocks
{
    public class MockPageFlowState : PageFlowState
    {
        Dictionary<string, byte[]> _data = new Dictionary<string, byte[]>();

        public override ICollection Keys
        {
            get { return _data.Keys; }
        }

        // Using serialization to imitate WF behavior
        public override object this[string key]
        {
            get
            {
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream((byte[])_data[key]);
                return formatter.Deserialize(stream);
            }
            set
            {
                if (value != null)
                {
                    MemoryStream stream = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, value);
                    _data[key] = stream.ToArray();
                }
            }
        }

        public override bool Contains(string key)
        {
            return _data.ContainsKey(key);
        }

        public override void Remove(string key)
        {
            _data.Remove(key);
        }
    }
}
